import * as tip from '@/utils/tip'

/*无需登录的白名单*/
const whiteList = [
	'/pages/auth/login',
	'/pages/auth/register',
	'/pages/index/index',
	'/pages/article/dynamic/index',
	'/pages/article/dynamic/detail',
	'/pages/splash/splash',
	'/pages/userCenter/index',
	'/pages/userCenter/map',
	'/pages/auth/protocol',
	'/pages/auth/privacy',
	'/pages/shop/goodsList',
	'/pages/shop/goodsDetail',
	'/pages/shop/index',
	'/pages/service/index',
	'/pages/service/sale',
	'/pages/service/purchase',
	'/pages/service/logistics',
	'/pages/service/inFee',
	'/pages/service/agent',
	'/pages/service/chain',
	'/pages/service/stock',
	'/pages/service/distribution',
	'/pages/trade/index',
	'/pages/shop/search',
	'/pages/webview/index'
]
const loginPage = '/pages/auth/login'

const list = ['navigateTo', 'switchTab', 'redirectTo', 'reLaunch']

const hasPermission = (url:string) => {
	for(let i in whiteList) {
		 if(url.indexOf(whiteList[i]) != -1) return true
	}
	return false
}

list.forEach(item => {
	uni.addInterceptor(item, {
		invoke(args:any) {
			const token = uni.getStorageSync('token')
			if(hasPermission(args.url)) return true
			if(token) {
				return true
			}
			uni.reLaunch({
				url:loginPage
			})
			return false
		},
		success(args:any) {
			//console.log(args)
		},
		fail(err) {
			console.log(err)
		}
	})
})

uni.addInterceptor('request', {
	invoke(args:any) {
		return true
	},
	success(args:any) {
		return true
	},
	fail(err) {
		console.log(err)
	}
})


